package com.macro.mall.tiny.modules.ums.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.common.service.RedisService;
import com.macro.mall.tiny.modules.ums.mapper.CourseMapper;
import com.macro.mall.tiny.modules.ums.model.Course;
import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.model.bo.CourseBO;
import com.macro.mall.tiny.modules.ums.model.bo.CourseEditBO;
import com.macro.mall.tiny.modules.ums.model.bo.CourseSelectBO;
import com.macro.mall.tiny.modules.ums.service.CourseService;
import com.macro.mall.tiny.security.util.JwtTokenUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: i_kun
 * @Desc: o_。
 * @create: 2025-06-26 09:21
 */
@Controller
@Tag(name = "CourseController",description = "课程管理模块")
@RequestMapping("/course")
public class CourseController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/inst", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult inst(HttpServletRequest request , @Validated @RequestBody CourseBO courseBO){
        //获取当前登陆人的信息
        String authHeader = request.getHeader(tokenHeader);
        //登陆人的名称
        String username = jwtTokenUtil.getUserNameFromToken(authHeader.substring(this.tokenHead.length()));
        courseBO.setOperator(username);
        courseService.inst(courseBO);
        return CommonResult.success(null);
    }
    
    @RequestMapping(value = "/instRedis", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult instRedis(HttpServletRequest request , @Validated @RequestBody CourseBO courseBO){
        //获取当前登陆人的信息
        String authHeader = request.getHeader(tokenHeader);
        //登陆人的名称
        String username = jwtTokenUtil.getUserNameFromToken(authHeader.substring(this.tokenHead.length()));
        courseBO.setOperator(username);
       return courseService.instRedis(courseBO);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Validated @RequestBody CourseEditBO courseEditBO){
        Course course  =  (Course) redisService.get("course_time");
        if (course.getCourseName().equals(courseEditBO.getCourseName())){
            return  CommonResult.failed("课程名称不能重复,不允许修改");
        }else {
            Course byId = courseService.getById(courseEditBO.getId());
            byId.setCourseName(courseEditBO.getCourseName());
            courseMapper.updateById(byId);
        }
        return null;
    }

    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public void delete(@RequestParam(value = "id",required = false) Long id){
        //查询一下 这个数据
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del",1);
        queryWrapper.eq("id",id);
        Course course = courseMapper.selectOne(queryWrapper);
        if (course!= null){
            return;
        }
        //TODO 不允许删除正在学习的课程
        //如果查询不到，就删除
        course.setIsDel(0);
        course.setUpdateBy("admin");
        course.setUpdateTime(new Date());
        courseMapper.updateById(course);
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    private CommonResult<CommonPage<Course>> list(CourseSelectBO courseSelectBO){
        if (courseSelectBO.getPageNum() == null){
            courseSelectBO.setPageNum(1);
        }
        if (courseSelectBO.getPageSize() == null){
            courseSelectBO.setPageSize(10);
        }
        return CommonResult.success(CommonPage.restPage(courseService.listPage(courseSelectBO)));
    }


    @RequestMapping(value = "/listDemo", method = RequestMethod.GET)
    @ResponseBody
    private CommonResult<CommonPage<Course>> listDemo(CourseSelectBO courseSelectBO){
        courseService.listDemo(courseSelectBO);
        return CommonResult.success(null);
    }


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    private void test(HttpServletRequest request , @Validated @RequestBody CourseBO courseBO){
//        //获取当前登陆人的信息
//        String authHeader = request.getHeader(tokenHeader);
//        //登陆人的名称
//        String username = jwtTokenUtil.getUserNameFromToken(authHeader.substring(this.tokenHead.length()));
//        courseBO.setOperator(username);
//        courseService.instRedisCourse(courseBO);
        courseService.delRedisKey();
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    private void export( HttpServletResponse response ,@Validated @RequestBody CourseBO courseBO){
        courseService.export(  response ,courseBO);
    }



    @RequestMapping(value = "/imports", method = RequestMethod.POST)
    @ResponseBody
    private void imports( MultipartFile file){

        courseService.imports(file);
    }


    @RequestMapping(value = "/exportDemo", method = RequestMethod.POST)
    @ResponseBody
    public void exportDemo(HttpServletResponse response ,@Validated @RequestBody CourseBO courseBO) throws IOException {

        courseService.exportDemo(response,courseBO);

    }
    @RequestMapping(value = "/importsDemo", method = RequestMethod.POST)
    private void importsDemo(MultipartFile file) throws Exception {
       courseService.importsDemo(file);
    }













}
